import { Popconfirm, Space, TableColumnsType, Tag, Tooltip, Typography, Image, Badge, Input, InputNumber } from 'antd'
import { NavigateFunction } from 'react-router-dom'
import type { ITableData, ITableAddType } from './types'
import { BadgeColorMap, BadgeColorDeliveryMap, DIS_STATUSBANDBTNTEXT, STATUSBAND, USERTYPETEXT } from '@/constant'
import { getImageByUrl } from '@/utils/getImageByUrl'
import { ORDER_STATUS_TEXT, RESERVE_INVOICE_STATUS_TEXT } from '@/constant/purchase'
import React from 'react'
import PermissionLink from '@/components/PermissionLink'
export const useIndexColumns = ({
  navigate,
  onView,
  onViewLogs,
  onConfirmDelivery,
  onSubmitPacking,
  onDel,
  isRoot,
}: {
  navigate: NavigateFunction
  onView: (info: ITableData) => void
  onViewLogs: (info: ITableData) => void
  onConfirmDelivery: (info: ITableData) => void
  onSubmitPacking: (info: ITableData) => void
  onDel: (info: ITableData) => void
  isRoot: boolean
}): TableColumnsType<ITableData> => [
  {
    title: '预约发货单',
    dataIndex: 'reserveInvoiceCode',
    key: 'reserveInvoiceCode',
    align: 'center',
    fixed: 'left',
    width: 250,
    render: (info) => {
      return info || '-'
    },
  },
  {
    title: '创建时间',
    dataIndex: 'createTime',
    key: 'createTime',
    align: 'center',
    width: 200,
    render: (info) => {
      return info || '-'
    },
  },
  {
    title: '供应链交期',
    dataIndex: 'deliveryTime',
    key: 'deliveryTime',
    align: 'center',
    width: 140,
    render: (info) => {
      return info || '-'
    },
  },
  {
    title: '供应商',
    dataIndex: 'supplierName',
    key: 'supplierName',
    align: 'center',
    width: 200,
    ellipsis: {
      showTitle: false,
    },
    render: (info) => {
      return info ? (
        <Tooltip placement="top" title={info}>
          {info}
        </Tooltip>
      ) : (
        '-'
      )
    },
  },
  {
    title: '供应链跟单',
    dataIndex: 'trackOrderPerson',
    key: 'trackOrderPerson',
    align: 'center',
    width: 100,
    render: (info) => {
      return info ? info : '-'
    },
  },
  {
    title: '订单状态',
    dataIndex: 'reserveInvoiceStatus',
    key: 'reserveInvoiceStatus',
    align: 'center',
    width: 120,
    render: (info) => {
      return <Badge color={BadgeColorDeliveryMap[info]} text={RESERVE_INVOICE_STATUS_TEXT[info] || '-'} /> || '-'
    },
  },
  {
    title: '推送状态',
    dataIndex: 'pushErrorMsg',
    key: 'pushErrorMsg',
    align: 'center',
    width: 150,
    hidden: !isRoot,
    render: (info) => {
      return info ? (
        <Tooltip placement="top" title={info}>
          <Tag color="error">推送CB失败</Tag>
        </Tooltip>
      ) : (
        '-'
      )
    },
  },
  {
    title: '预约发货时间',
    dataIndex: 'scheduleDeliveryTime',
    key: 'scheduleDeliveryTime',
    align: 'center',
    width: 180,
    render: (info) => {
      return info || '-'
    },
  },
  {
    title: '预计到货时间',
    dataIndex: 'estimatedArrivalTime',
    key: 'estimatedArrivalTime',
    align: 'center',
    width: 120,
    render: (info) => {
      return info || '-'
    },
  },
  {
    title: '箱数',
    dataIndex: 'totalBoxQty',
    key: 'totalBoxQty',
    align: 'center',
    width: 100,
    render: (info) => {
      return info || '-'
    },
  },
  {
    title: '预约总数',
    dataIndex: 'totalScheduleQty',
    key: 'totalScheduleQty',
    align: 'center',
    width: 100,
    render: (info) => {
      return info || '-'
    },
  },
  {
    title: '已扫码数量',
    dataIndex: 'totalScanQty',
    key: 'totalScanQty',
    align: 'center',
    width: 100,
    render: (info) => {
      return info == 0 ? 0 : info || '-'
    },
  },
  {
    title: '到货总数',
    dataIndex: 'totalArriveQty',
    key: 'totalArriveQty',
    align: 'center',
    width: 100,
    render: (info) => {
      return info || '-'
    },
  },
  {
    title: '最近到货时间',
    dataIndex: 'lastArrivalTime',
    key: 'lastArrivalTime',
    align: 'center',
    width: 180,
    render: (info) => {
      return info || '-'
    },
  },
  {
    title: '物流信息',
    dataIndex: 'status',
    key: 'status',
    align: 'center',
    fixed: 'right',
    width: 100,
    render: (_, rows) => {
      return (
        <Space size={'large'}>
          <Typography.Link onClick={() => onView(rows)}>查看</Typography.Link>
        </Space>
      )
    },
  },
  {
    title: '操作',
    fixed: 'right',
    align: 'center',
    width: 250,
    render: (_, rows) => {
      return (
        <Space size={'large'}>
          {rows.reserveInvoiceStatus === 2 ? (
            <PermissionLink permission="confirmDeliver" onClick={() => onConfirmDelivery(rows)}>
              确认发货
            </PermissionLink>
          ) : null}
          {rows.reserveInvoiceStatus === 6 ? (
            <PermissionLink permission="submitPacking" onClick={() => onSubmitPacking(rows)}>
              提交装箱
            </PermissionLink>
          ) : null}
          {rows.reserveInvoiceStatus != 4 && rows.reserveInvoiceStatus != 5 ? (
            <PermissionLink permission="cancelDeliver" type="danger" onClick={() => onDel(rows)}>
              取消预约
            </PermissionLink>
          ) : null}
          {isRoot ? <Typography.Link onClick={() => onViewLogs(rows)}>查看日志</Typography.Link> : null}
        </Space>
      )
    },
  },
]

export const useInnerColumns = (): TableColumnsType<ITableData> => [
  {
    title: '采购订单号',
    dataIndex: 'purchaseOrderCode',
    key: 'purchaseOrderCode',
    align: 'center',
    render: (info) => {
      return info || '-'
    },
  },
  {
    title: '款式编码',
    dataIndex: 'productSimpleVO',
    key: 'spuCode',
    align: 'center',
    render: (info) => {
      return info?.spuCode || '-'
    },
  },
  {
    title: '助记码',
    dataIndex: 'productSimpleVO',
    key: 'designCode',
    align: 'center',
    render: (info) => {
      return info?.designCode || '-'
    },
  },
  {
    title: '工厂款号',
    dataIndex: 'productSimpleVO',
    key: 'factorySpuCode',
    align: 'center',
    render: (info) => {
      return info?.factorySpuCode || '-'
    },
  },
  {
    title: '款式名称',
    dataIndex: 'productSimpleVO',
    key: 'spuName',
    align: 'center',
    render: (info) => {
      return info?.spuName || '-'
    },
  },
  {
    title: '商品分类',
    dataIndex: 'productSimpleVO',
    key: 'className',
    align: 'center',
    render: (info) => {
      return info?.className || '-'
    },
  },
  {
    title: '商品条码',
    dataIndex: 'productSimpleVO',
    key: 'skuCode',
    align: 'center',
    render: (info) => {
      return info?.skuCode || '-'
    },
  },
  {
    title: '尺码',
    dataIndex: 'productSimpleVO',
    key: 'size',
    align: 'center',
    render: (info) => {
      return info?.size || '-'
    },
  },
  {
    title: '颜色',
    dataIndex: 'productSimpleVO',
    key: 'colorDetailName',
    align: 'center',
    render: (info) => {
      return info?.colorDetailName || '-'
    },
  },
  {
    title: '预约发货数量',
    dataIndex: 'scheduleQty',
    key: 'scheduleQty',
    align: 'center',
    render: (info) => {
      return info || '-'
    },
  },
  {
    title: '已扫码数量',
    dataIndex: 'scanQty',
    key: 'scanQty',
    align: 'center',
    width: 150,
    render: (info) => {
      return info == 0 ? 0 : info || '-'
    },
  },
  {
    title: '内箱数',
    dataIndex: 'productSimpleVO',
    key: 'inPackageNum',
    align: 'center',
    render: (info) => {
      return info?.inPackageNum || '-'
    },
  },
  {
    title: '外箱数',
    dataIndex: 'productSimpleVO',
    key: 'outPackageNum',
    align: 'center',
    render: (info) => {
      return info?.outPackageNum || '-'
    },
  },
  {
    title: '箱数',
    dataIndex: 'boxQty',
    key: 'boxQty',
    align: 'center',
    render: (info) => {
      return info || '-'
    },
  },
  {
    title: '到货数量',
    dataIndex: 'arriveQty',
    key: 'arriveQty',
    align: 'center',
    render: (info) => {
      return info || '-'
    },
  },
]

export const useSendColumns = ({
  tableData,
  settableData,
  selectedRowKeys,
}: {
  tableData: ITableAddType[]
  settableData: React.Dispatch<React.SetStateAction<ITableAddType[]>>
  selectedRowKeys: React.Key[]
}): TableColumnsType<ITableData> => [
  {
    title: '供应商',
    dataIndex: 'supplierName',
    key: 'supplierName',
    align: 'center',
    width: 250,
    ellipsis: {
      showTitle: false,
    },
    render: (info) => {
      return info ? (
        <Tooltip placement="top" title={info}>
          {info}
        </Tooltip>
      ) : (
        '-'
      )
    },
  },
  {
    title: '采购订单号',
    dataIndex: 'purchaseOrderCode',
    key: 'purchaseOrderCode',
    align: 'center',
    width: 220,
    render: (info) => {
      return info || '-'
    },
  },
  {
    title: '供应链交期',
    dataIndex: 'deliveryTime',
    key: 'deliveryTime',
    align: 'center',
    width: 140,
    render: (info) => {
      return info || '-'
    },
  },
  {
    title: '款式编码',
    dataIndex: 'spuCode',
    key: 'spuCode',
    align: 'center',
    width: 140,
    render: (info) => {
      return info || '-'
    },
  },
  {
    title: '助记码',
    dataIndex: 'memoryCode',
    key: 'memoryCode',
    align: 'center',
    width: 140,
    render: (info) => {
      return info || '-'
    },
  },
  {
    title: '工厂款号',
    dataIndex: 'factorySpuCode',
    key: 'factorySpuCode',
    align: 'center',
    width: 140,
    render: (info) => {
      return info || '-'
    },
  },
  {
    title: '商品名称',
    dataIndex: 'productName',
    key: 'productName',
    align: 'center',
    width: 200,
    render: (info) => {
      return info || '-'
    },
  },
  {
    title: '商品分类',
    dataIndex: 'wholeClassName',
    key: 'wholeClassName',
    align: 'center',
    width: 200,
    render: (info) => {
      return info || '-'
    },
  },
  {
    title: '商品条码',
    dataIndex: 'skuCode',
    key: 'skuCode',
    align: 'center',
    width: 160,
    render: (info) => {
      return info || '-'
    },
  },
  {
    title: '尺码',
    dataIndex: 'commoditySize',
    key: 'commoditySize',
    align: 'center',
    width: 100,
    render: (info) => {
      return info || '-'
    },
  },
  {
    title: '颜色',
    dataIndex: 'colorDetailName',
    key: 'colorDetailName',
    align: 'center',
    width: 120,
    render: (info) => {
      return info || '-'
    },
  },
  {
    title: '吊牌价',
    dataIndex: 'tagPrice',
    key: 'tagPrice',
    align: 'center',
    width: 120,
    render: (info) => {
      return info || '-'
    },
  },
  // {
  //   title: '采购价',
  //   dataIndex: 'purchasePrice',
  //   key: 'purchasePrice',
  //   align: 'center',
  //   width: 200,
  //   render: (info) => {
  //     return info || '-'
  //   },
  // },
  {
    title: '下单数量',
    dataIndex: 'orderQty',
    key: 'orderQty',
    align: 'center',
    fixed: 'right',
    width: 120,
    render: (info) => {
      return info || 0
    },
  },
  {
    title: '已预约数量',
    dataIndex: 'scheduledQty',
    key: 'scheduledQty',
    align: 'center',
    fixed: 'right',
    width: 120,
    render: (info) => {
      return info || 0
    },
  },
  {
    title: '待预约数量',
    dataIndex: 'dyy',
    key: 'dyy',
    align: 'center',
    fixed: 'right',
    width: 120,
    render: (_, rows) => {
      return rows.orderQty - rows.scheduledQty >= 0 ? rows.orderQty - rows.scheduledQty : 0
    },
  },
  {
    title: '本次预约数量',
    dataIndex: 'scheduleQty',
    key: 'scheduleQty',
    align: 'center',
    fixed: 'right',
    width: 120,
    render: (info, rows, index) => {
      return selectedRowKeys?.includes(rows?.id) ? (
        <InputNumber
          defaultValue={info}
          onChange={(value: any) => {
            if (value <= 0) return
            rows['isEdit'] = false
            rows.scheduleQty = Math.ceil(value)
            settableData([...tableData])
          }}
          precision={0}
          min={1}
        ></InputNumber>
      ) : (
        <div
          className="edit_intable_style"
          onClick={() => {
            rows['isEdit'] = true
            settableData([...tableData])
          }}
        >
          {info}
        </div>
      )
    },
  },
]
